package com.icez.banner_view

import android.content.Context
import android.view.LayoutInflater
import android.view.ViewGroup
import androidx.databinding.DataBindingUtil
import androidx.databinding.ViewDataBinding
import androidx.recyclerview.widget.RecyclerView

/**
 * RecyclerView 适配器基类
 * @author Icez
 */
abstract class BannerViewAdapter<T,VB:ViewDataBinding>(val mLayoutResId:Int, val mData:ArrayList<T>?):
    RecyclerView.Adapter<RecyclerViewHolder>() {

    var mIsInfiniteLoop:Boolean = false

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): RecyclerViewHolder {
        val binding = DataBindingUtil.inflate<VB>(LayoutInflater.from(parent.context),mLayoutResId,parent,false)
        return RecyclerViewHolder(binding)
    }

    override fun onBindViewHolder(holder: RecyclerViewHolder, position: Int) {
        if(mIsInfiniteLoop && (mData?.size?:0)>1){
            onBusinessOperation(holder.mBinding.root.context,holder.mBinding as VB,position%(mData?.size?:0),mData?.get(position%(mData?.size?:0)))
        }else{
            onBusinessOperation(holder.mBinding.root.context,holder.mBinding as VB,position,mData?.get(position))
        }

    }

    override fun getItemCount(): Int {
        if(mIsInfiniteLoop && (mData?.size?:0)>1){
            return Int.MAX_VALUE
        }else{
            return mData?.size?:0
        }

    }



    /**
     * 业务操作
     *
     * @param context 上下文
     * @param binding databinding
     * @param position 索引
     * @param item 数据
     */
    abstract fun onBusinessOperation(context: Context, binding:VB, position: Int, item:T?)
}